#!/bin/sh
set -o xtrace
set -o errexit

sed -i -e "s/enabled=0/enabled=1/" /etc/yum.repos.d/CentOS-Base.repo
sed -i -e "s/enabled=1/enabled=0/" /etc/yum.repos.d/Citrix.repo

# Install basics for vi and git
yum -y  --enablerepo=base install gcc make vim-enhanced zlib-devel openssl-devel curl-devel.i386 expat-devel

# Simple but usable vimrc
if [ ! -e /root/.vimrc ]; then
    cat > /root/.vimrc <<EOF
syntax on
se ts=4
se expandtab
se shiftwidth=4
EOF
fi

# Use the pretty vim
if [ -e /usr/bin/vim ]; then
    rm /bin/vi
    ln -s /usr/bin/vim /bin/vi
fi

# Install git
if ! which git; then
    DEST=/tmp/
    GITDIR=$DEST/git-1.7.7
    cd $DEST
    rm -rf $GITDIR*
    wget http://git-core.googlecode.com/files/git-1.7.7.tar.gz
    tar xfv git-1.7.7.tar.gz
    cd $GITDIR
    ./configure --with-curl --with-expat
    make install
fi

# Clone devstack
DEVSTACK=${DEVSTACKROOT:-"/root/devstack"}
if [ ! -d $DEVSTACK ]; then
    git clone git://github.com/openstack-dev/devstack.git $DEVSTACK
fi

# Clone nova
NOVA=${NOVA:-"/root/nova"}
if [ ! -d $NOVA ]; then
	git clone https://github.com/openstack/nova.git
	cd nova
	git checkout stable/essex
	cp plugins/xenserver/xenapi/etc/xapi.d/plugins/* /etc/xapi.d/plugins
fi

sed -i -e "s/enabled=1/enabled=0/" /etc/yum.repos.d/CentOS-Base.repo
sed -i -e "s/enabled=0/enabled=1/" /etc/yum.repos.d/Citrix.repo